<?php
/**
 * 用户控制器
 */
namespace app\merchant\controller;
use app\admin\model\Qiniu;
use think\Request;
use app\admin\model\Img;
class ImgController extends Controller
{
    //列表
    public function index(Request $request)
    {
        $param = $request->param();
        $this->assign([
            'type'  => $param['type'],
            'name'  => $param['name'],
        ]);
        return $this->fetch();
    }
    public function update(Request $request,Img $Img)
    {
        $w[] = ['merchant_id','eq','11'];
        $data =  $Img->where($w)->select();
        $result = json_encode(array(
            'state' => count($data) ? 'SUCCESS':'ERROR',
            'list' => $data
        ));

        echo $result;
    }
    public function addimg(Request $request,Img $Img)
    {
        $Qiniu = new Qiniu();
        $file = $_FILES;
        $uid = $this->uid;
        $list = $Qiniu->upload($file);
        $data['merchant_id'] = $uid;
        $data['add_time'] = time();
        $data['qn_url'] = $list['key']['url'];
        $data['qn_key'] = $list['key']['key'];
        $AA = $Img->insert($data);
        $return_data['title'] = 'banner';
        $return_data['original'] = ''; // 这里好像没啥用 暂时注释起来
        $return_data['state'] = 'SUCCESS';
        $return_data['path'] = 'images';
        $return_data['url'] = $data['qn_url'];
        $this->ajaxReturn($return_data);
    }
    public function ajaxReturn($data,$type = 'json'){
        exit(json_encode($data,JSON_UNESCAPED_UNICODE));
    }
    public function aa(Request $request)
    {
        $Img = new Img();
        $param = $request->param();
        $name = $param['qn_key'];
        $Qiniu = new Qiniu();
        $list = $Qiniu->del($name);
        if ($list['code']==1){
            $data  = $Img->where('id='.$param['id'])->delete();
            if ($data){
                $arr['code'] = '1';
                $arr['msg'] = '删除成功';
                $this->ajaxReturn($arr);
            }else{
                $arr['code'] = '2';
                $arr['msg'] = '图片已删除';
                $this->ajaxReturn($arr);
            }
        }else{
            $arr['code'] = '3';
            $arr['msg'] = '删除失败';
            $this->ajaxReturn($arr);
        }
    }
}
//ssssasdadad
//sdasdwqq
